﻿#region copyright info
//------------------------------------------------------------------------------
// <copyright company="ChaosStudio">
//     Copyright (c) 2002-2010 巧思工作室.  All rights reserved.
//     Contact:		MSN:zhouyu@cszi.com , QQ:478779122
//		Link:				http://www.69sea.com http://www.cszi.com
// </copyright>
//------------------------------------------------------------------------------
#endregion
using System;
using System.Collections.Generic;

namespace CS.Xml.Maps
{
    /// <summary>
    ///   枚举类型映射管理器
    /// </summary>
    /// 
    /// <description class = "CS.Xml.Maps.EnumMapper">
    ///   
    /// </description>
    /// 
    /// <history>
    ///   2010-9-14 13:09:03 , zhouyu ,  创建	     
    ///  </history>
    public class EnumMapper : XmlMapsBase
    {
        private const string ENUM_CONFIG_FILE = "Config/EnumMaps.config";


        /// <summary>
        /// 获取枚举的 键(定义名)-名称 字典
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static IDictionary<string,string > GetKeyName(Type type)
        {
            return GetKeyName(ENUM_CONFIG_FILE, type);
        }

        /// <summary>
        /// 获取枚举的 值(数值)-名称 字典
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static IDictionary<int,string > GetValueName(Type type)
        {
            return GetValueName(ENUM_CONFIG_FILE, type);
        }

        /// <summary>
        /// 获取枚举的 键(定义名)-名称 字典
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static IDictionary<string, string> GetKeyName(string file, Type type)
        {
            if(!type.IsEnum) return null;
            var xml = LoadConfig(file);
            var nodes = xml.SelectNodes(string.Format("configuration/{0}/row", type.Name));
            if (nodes != null)
            {
                var dic = new Dictionary<string, string>();
                var names = Enum.GetNames(type);
                foreach (var name in names)
                {
                    var node = NodeFind(nodes, "key", name);
                    if(node!=null)
                    {
                        var xName = node.Attributes["name"].Value;
                        dic[name] = string.IsNullOrEmpty(xName) ? name : xName;
                    }
                    else
                    {
                        dic[name] = name;
                    }
                }
                return dic;
            }
            return null;
        }

        /// <summary>
        /// 获取枚举的 值(数值)-名称 字典
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static IDictionary<int, string> GetValueName(string file, Type type)
        {
            if (!type.IsEnum) return null;
            var xml = LoadConfig(file);
            var nodes = xml.SelectNodes(string.Format("configuration/{0}/row", type.Name));
            if (nodes != null)
            {
                var dic = new Dictionary<int, string>();
                var values = Enum.GetValues(type);
                foreach (var value in values)
                {
                    var tv = (int)value;
                    var node = NodeFind(nodes, "value", tv.ToString());
                    if (node != null)
                    {
                        var xName = node.Attributes["name"].Value;
                        dic[tv] = string.IsNullOrEmpty(xName) ? value.ToString() : xName;
                    }
                    else
                    {
                        dic[tv] = value.ToString();
                    }
                }
                return dic;
            }
            return null;
        }
     
       

    }
}
